<template>
  <div class="data-list" :style="{ 'max-height': maxHeight }">
    <div v-for="(item, index) in resData" class="data-item">
      <div
        :class="item?.reported ? 'detail-header detail-header-count' : 'detail-header'"
        @click="checkedClick(item, index)"
      >
        <div class="header-detail-left">
          <a-checkbox v-model:checked="item.checked" class="check-single"></a-checkbox>
          <i class="iconfont icon-member4" style="margin-left: 8px; color: #00000073"></i>
          <a-tooltip placement="bottomLeft" v-if="item.commentator?.length > 4">
            <template #title>
              <span style="margin-left: 8px">{{ item.commentator }}</span>
            </template>
            <a
              :class="{ disable: !btnPerm.includes('merberCommentAble') }"
              v-if="!item.anonymous"
              style="margin-left: 8px"
              @click.stop="toSingleMember(item)"
              >{{
                item.commentator.length > 4
                  ? item.commentator.slice(0, 4) + "..."
                  : item.commentator
              }}</a
            >
          </a-tooltip>
          <a
            :class="{ disable: !btnPerm.includes('merberCommentAble') }"
            v-else="!item.anonymous"
            style="margin-left: 8px"
            @click.stop="toSingleMember(item)"
            >{{ item.commentator }}</a
          >

          <span class="anony-box" v-if="item.anonymous" style="margin-left: 8px"
            >游客</span
          >

          <div class="ip-box">
            （IP：<a
              :class="{ disable: !btnPerm.includes('IPCommentAble') }"
              @click.stop="toSingleIp(item)"
              >{{ item.ipAddress }}</a
            >）
          </div>
          <div class="time marginL40_16">
            <i class="iconfont icon-time" style="color: #00000073"></i>
            <span>{{ item.createTime }}</span>
          </div>
          <div class="status status1" v-show="item.auditStatus === 0 && !item?.reported">
            待审核
          </div>
          <div class="status status2" v-show="item.auditStatus === 1 && !item?.reported">
            审核通过
          </div>
          <div class="status status3" v-show="item.auditStatus === 2 && !item?.reported">
            审核不通过
          </div>
          <div class="status status3" v-show="item.isUserSilenced && !item?.reported">
            用户禁止评论
          </div>
          <div class="status status3" v-show="item.isIpSilenced && !item?.reported">
            IP禁止评论
          </div>
          <div class="status4 marginL40_16" v-show="item?.reported">
            <i class="iconfont icon-report"></i>
            <span>举报</span>
          </div>
        </div>
        <div class="header-detail-right" v-if="item?.reported">
          <div class="reports-name marginL40_16" v-if="item?.lastReport">
            举报人: {{ item?.lastReport?.userName }}[IP:{{ item?.lastReport?.ipAddress }}]
          </div>
          <div class="reports-time marginL40_16" v-if="item?.lastReport">
            举报时间: {{ item?.lastReport?.createTime }}
          </div>
          <div
            class="dots marginL40_16"
            v-if="item?.reportedCnt > 1"
            @click.stop="openReporterModel(item.id)"
          >
            ...
            <a-badge :count="item?.reportedCnt">
              <a href="#" class="head-example" />
            </a-badge>
          </div>
        </div>
      </div>
      <div class="detail-content">
        <div class="commentator-comment1">
          <div class="commentator">
            <span
              class="comment-left"
              :style="{
                'min-width':
                  (item.type === 3
                    ? item.commentator.length + item.originalCommentator.length + 2
                    : 2) *
                    16 +
                  'px',
              }"
            >
              {{
                item.type === 3
                  ? `${item.commentator} 评论 ${item.originalCommentator}:`
                  : item.type === 1
                  ? "评论:"
                  : ""
              }}
            </span>
            <span class="comment">{{ item.comment }}</span>
          </div>
          <div v-if="!item?.reported" class="like-box">
            <i class="iconfont icon-a-Firstpush"></i>
            <span class="like-count">{{
              item.likeCnt > 1000 ? `${(item.likeCnt / 1000).toFixed(1)}K` : item.likeCnt
            }}</span>
          </div>
        </div>
        <div class="comment-box">
          <div v-if="item?.reported" class="commentator-comment">
            <div class="commentator">原因:</div>
            <span class="comment">{{
              reason[item?.lastReport?.reportType - 1] || item?.lastReport?.extraMsg
            }}</span>
          </div>
        </div>
        <div
          v-if="
            !item?.reported && (item?.sysAdminReplyDto?.comment || item?.changeReplied)
          "
          class="replied"
        >
          <div class="replied-content-box">
            <span
              class="replied-text"
              :style="{ 'margin-top': !item.changeReplied ? 0 : '5px' }"
              >回复:</span
            >
            <div class="replied-content-change" v-if="item.changeReplied">
              <a-textarea
                class="text-input"
                v-model:value="item.replyCommentCon"
                placeholder="不超过200个字"
                allowClear
                show-count
                auto-size
                :maxlength="200"
              />
              <div class="buttons">
                <a-button
                  @click="
                    () => {
                      (item.changeReplied = false), (item.replyCommentCon = '');
                    }
                  "
                  class="button"
                  style="margin-right: 8px"
                >
                  取消
                </a-button>
                <a-button
                  @click="replyComment(item, item.id)"
                  type="primary"
                  class="button"
                >
                  回复
                </a-button>
              </div>
            </div>
            <div class="replied-content" v-else>
              {{ item?.sysAdminReplyDto?.comment }}
            </div>
          </div>
          <div class="like-box" v-if="!item.changeReplied">
            <i class="iconfont icon-a-Firstpush"></i>
            <span class="like-count">{{
              item?.sysAdminReplyDto?.likeCnt > 1000
                ? `${(item?.sysAdminReplyDto?.likeCnt / 1000).toFixed(1)}K`
                : item?.sysAdminReplyDto?.likeCnt
            }}</span>
          </div>
        </div>
      </div>
      <div
        :class="props.isContent ? 'detail-footer detail-content-footer' : 'detail-footer'"
      >
        <div class="footer-left" v-show="!props.isContent">
          <div class="channal-name">
            [{{ item.channelName }}]
            <a-tooltip>
              <template #title>{{ item.title }}</template>
              <span @click="toScanUrl(item)" class="title">{{ item.title }}</span>
            </a-tooltip>
          </div>
          <template v-if="props.tabKey === 1">
            <a
              :class="{ disable: !btnPerm.includes('allCommentAble') }"
              @click.stop="toAllComment(item)"
              >内容全部评论</a
            >
            <div class="all-count">: {{ item.commentCnt }}</div>
          </template>
        </div>
        <div class="footer-right">
          <template v-if="props.tabKey === 1">
            <a-popconfirm
              v-if="item.auditStatus !== 1"
              placement="topRight"
              ok-text="确定"
              cancel-text="取消"
              :disabled="!btnPerm.includes('auditPassAble')"
              @confirm="checkPass(item.id)"
            >
              <template #title>
                <p style="width: 190px">是否确定审核通过？</p>
              </template>
              <a :class="{ disable: !btnPerm.includes('auditPassAble') }">审核通过</a>
            </a-popconfirm>
            <a-popconfirm
              v-if="item.auditStatus !== 2"
              placement="topRight"
              ok-text="确定"
              cancel-text="取消"
              :disabled="!btnPerm.includes('auditNoPassAble')"
              @confirm="checkUnPass(item.id)"
            >
              <template #title>
                <p style="width: 190px">是否确定审核不通过？</p>
              </template>
              <a :class="{ disable: !btnPerm.includes('auditNoPassAble') }">审核不通过</a>
            </a-popconfirm>
            <a-popconfirm
              v-if="!item.isUserSilenced"
              placement="topRight"
              ok-text="确定"
              cancel-text="取消"
              :disabled="!btnPerm.includes('noAllowUserCommentsAble')"
              @confirm="forbidUserComment(item, 1)"
            >
              <template #title>
                <p style="width: 190px">是否确定禁止用户评论？</p>
              </template>
              <a :class="{ disable: !btnPerm.includes('noAllowUserCommentsAble') }"
                >禁止用户评论</a
              >
            </a-popconfirm>
            <a
              :class="{ disable: !btnPerm.includes('allowUserCommentsAble') }"
              v-if="item.isUserSilenced"
              @click="forbidUserComment(item, 2)"
              >允许用户评论</a
            >

            <a-popconfirm
              v-if="!item.isIpSilenced"
              placement="topRight"
              ok-text="确定"
              cancel-text="取消"
              :disabled="!btnPerm.includes('noAllowIPCommentsAble')"
              @confirm="forbidIpComment(item, 1)"
            >
              <template #title>
                <p style="width: 190px">是否确定禁止IP评论？</p>
              </template>
              <a :class="{ disable: !btnPerm.includes('noAllowIPCommentsAble') }"
                >禁止IP评论</a
              >
            </a-popconfirm>
            <a
              :class="{ disable: !btnPerm.includes('AllowIPCommentsAble') }"
              v-if="item.isIpSilenced"
              @click="forbidIpComment(item, 2)"
              >允许IP评论</a
            >
            <a
              :class="{ disable: !btnPerm.includes('replyAble') }"
              @click="toShowReply(item.id)"
              v-show="!item?.sysAdminReplyDto?.comment && item.auditStatus !== 2"
              >回复</a
            >
            <a
              :class="{ disable: !btnPerm.includes('changeReplyAble') }"
              @click="changeShowReply(item.id)"
              v-show="
                item?.sysAdminReplyDto?.comment &&
                !item.changeReplied &&
                item.auditStatus !== 2
              "
              >修改回复</a
            >
            <a-popconfirm
              v-if="props.tabKey === 1"
              placement="topRight"
              ok-text="确定"
              cancel-text="取消"
              :disabled="!btnPerm.includes('deleteAble')"
              @confirm="toDetele(item.id)"
            >
              <template #title>
                <p style="width: 190px">是否确定删除该评论？</p>
              </template>
              <a :class="{ disable: !btnPerm.includes('deleteAble') }">删除</a>
            </a-popconfirm>
          </template>
          <template v-if="props.tabKey === 3">
            <a-popconfirm
              placement="topRight"
              ok-text="确定"
              :disabled="!btnPerm.includes('cancelReportAble')"
              cancel-text="取消"
              @confirm="cancelReport(item.id)"
            >
              <template #title>
                <p style="width: 190px">是否确定审核通过？</p>
              </template>
              <a :class="{ disable: !btnPerm.includes('cancelReportAble') }">取消举报</a>
            </a-popconfirm>
            <a-popconfirm
              placement="topRight"
              ok-text="确定"
              cancel-text="取消"
              :disabled="!btnPerm.includes('deleteCommentAble')"
              @confirm="deleteComment(item.id)"
            >
              <template #title>
                <p style="width: 190px">是否确定删除评论？</p>
              </template>
              <a :class="{ disable: !btnPerm.includes('deleteCommentAble') }">删除评论</a>
            </a-popconfirm>
          </template>
        </div>
      </div>
    </div>
  </div>
  <base-modal
    :width="modAlProps.width"
    :isVisible="isOpenReportModel"
    :title="modAlProps.title"
    :isCancleBtn="true"
    :buttons="[]"
    :cancleText="'关闭'"
    @handleCancle="isOpenReportModel = false"
  >
    <template v-slot:body>
      <div class="baseTable">
        <baseTable
          :rowSelection="1"
          :columns="columns.reporterListColumn"
          :dataSource="reporterList"
          :scroll="{ y: 340 }"
          :isShowPagination="false"
          :showCheckDelete="false"
        >
        </baseTable>
      </div>
    </template>
  </base-modal>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, watch } from "vue";
import { cloneDeep, stubString } from "lodash";
import baseTable from "@/components/table/baseTable.vue";
import baseModal from "@/components/modal/baseModal.vue";
import columns from "@/components/table/tableConfig/columnsName";
import request from "@/api/index";
import { routerPerm } from "@/utils/common";
//props
const props = defineProps({
  configData: {
    type: Object,
    default: {},
  },
  isOpenSelectOption: {
    type: Boolean,
    default: false,
  },
  isContent: {
    type: Boolean,
    default: false,
  },
  tabKey: {
    type: Number,
    default: 1,
  },
});
//emit
const emit = defineEmits([
  "checkedClick",
  "toSingleMember",
  "toSingleIp",
  "toAllComment",
  "checkPass",
  "checkUnPass",
  "forbidUserComment",
  "forbidIpComment",
  "toReply",
  "changeReply",
  "toDetele",
  "cancelReport",
  "deleteComment",
]);

//data
const btnPerm = ref<any[]>([]);
const resData = ref(cloneDeep(props.configData.records));
const isOpenReportModel = ref<boolean>(false);
const reporterList = ref<any[]>([]);
const maxHeight = ref<string>("");
const modAlProps: any = reactive({
  width: "M",
  title: "举报",
});
const reason = [
  "营销广告",
  "违法涉政",
  "淫秽色情",
  "网络赌博",
  "诈骗",
  "言论攻击",
  "网络谣言",
  "民族宗教",
  "涉未成年举报",
];
//watch
watch(
  () => props.configData.records,
  (newVal, oldVal) => {
    if (newVal.length) {
      resData.value = cloneDeep(newVal);
      resData.value.length &&
        resData.value.forEach((item: any) => {
          if (item?.sysAdminReplyDto?.comment) {
            item.replyCommentCon = item?.sysAdminReplyDto?.comment;
          } else {
            item.replyCommentCon = "";
          }
        });
    } else {
      resData.value = [];
    }
  },
  { deep: true }
);

watch(
  () => props.isOpenSelectOption,
  (newVal, oldVal) => {
    maxHeight.value = `${
      props.isOpenSelectOption
        ? document.documentElement.clientHeight - 460
        : document.documentElement.clientHeight - 260
    }px`;
  }
);

//method

const openReporterModel = async (commentId: number) => {
  reporterList.value = await request.fetchCommentCommentReports({ commentId, size: 100 });
  reporterList.value.length &&
    reporterList.value.forEach((item: any) => {
      if (item.reportType) {
        item.reportTypeText = reason[item.reportType];
      } else {
        item.reportTypeText = item.extraMsg;
      }
    });
  isOpenReportModel.value = true;
};

//单击事件
const checkedClick = (item: any, index: number) => {
  emit("checkedClick", item, index);
};

// 单个会员评论
const toSingleMember = (item: any) => {
  if (!btnPerm.value.includes("merberCommentAble")) return;
  emit("toSingleMember", item);
};

//单个IP评论
const toSingleIp = (item: any) => {
  if (!btnPerm.value.includes("IPCommentAble")) return;
  emit("toSingleIp", item);
};

//浏览内容
const toScanUrl = (item: any) => {
  console.log(item.scanUrl);
  window.open(item.scanUrl);
};

//前往内容全部评论
const toAllComment = (item: any) => {
  if (!btnPerm.value.includes("allCommentAble")) return;
  emit("toAllComment", item);
};

//审核通过
const checkPass = (id: number) => {
  emit("checkPass", [id]);
};
//审核不通过
const checkUnPass = (id: number) => {
  emit("checkUnPass", [id]);
};
//禁止用户评论/允许用户评论
const forbidUserComment = (item: any, type: number) => {
  if (type == 1) {
    if (!btnPerm.value.includes("noAllowUserCommentsAble")) return;
  } else if (type == 2) {
    if (!btnPerm.value.includes("allowUserCommentsAble")) return;
  }
  emit("forbidUserComment", item);
};
//禁止IP评论
const forbidIpComment = (item: any, type: number) => {
  if (type == 1) {
    if (!btnPerm.value.includes("noAllowIPCommentsAble")) return;
  } else if (type == 2) {
    if (!btnPerm.value.includes("AllowIPCommentsAble")) return;
  }
  emit("forbidIpComment", item);
};
//取消举报
const cancelReport = (id: number) => {
  emit("cancelReport", [id]);
};
//删除评论
const deleteComment = (id: number) => {
  emit("deleteComment", [id]);
};

const toShowReply = (id: number) => {
  if (!btnPerm.value.includes("replyAble")) return;
  resData.value.length &&
    resData.value.forEach((item: any) => {
      if (item.id === id) {
        item.changeReplied = true;
      } else {
        item.changeReplied = false;
      }
    });
};
const changeShowReply = (id: number) => {
  if (!btnPerm.value.includes("changeReplyAble")) return;
  resData.value.length &&
    resData.value.forEach((item: any) => {
      if (item.id === id) {
        item.changeReplied = true;
        item.replyCommentCon = item?.sysAdminReplyDto?.comment;
      } else {
        item.changeReplied = false;
      }
    });
};

//回复判断
const replyComment = (item: any, id: number) => {
  if (item.replyCommentCon) {
    if (item?.sysAdminReplyDto?.id) {
      changeReply(item.replyCommentCon, item?.sysAdminReplyDto?.id);
    } else {
      toReply(item.replyCommentCon, id);
    }
  }
};
//回复
const toReply = (comment: string, id: number) => {
  emit("toReply", comment, id);
};
//更改回复
const changeReply = (comment: string, id: number) => {
  emit("changeReply", comment, id);
};
//删除
const toDetele = (id: number) => {
  emit("toDetele", [id]);
};

onMounted(() => {
  btnPerm.value = routerPerm();
  maxHeight.value = `${
    props.isOpenSelectOption
      ? document.documentElement.clientHeight - 460
      : document.documentElement.clientHeight - 268
  }px`;
  window.onresize = () => {
    maxHeight.value = `${
      props.isOpenSelectOption
        ? document.documentElement.clientHeight - 460
        : document.documentElement.clientHeight - 268
    }px`;
  };
});
</script>

<style lang="less" scoped>
.data-list {
  display: flex;
  margin-top: 16px;
  flex-direction: column;
  overflow-y: scroll;

  .data-item {
    margin-top: 16px;
    display: flex;
    flex-direction: column;
    border: 1px solid rgba(0, 0, 0, 0.06);

    .detail-header {
      display: flex;
      flex-direction: row;
      align-items: center;
      padding: 6px 9px;
      background: rgba(0, 0, 0, 0.02);

      .header-detail-left {
        display: flex;
        flex-direction: row;
        align-items: center;
      }

      .header-detail-right {
        display: flex;
        flex-direction: row;
        align-items: center;

        .reports-name {
          display: block;
          font-size: 14px;
          line-height: 14px;
          color: #000000d9;
        }

        .reports-time {
          display: block;
          font-size: 14px;
          line-height: 14px;
          color: #000000d9;
        }
      }

      .dots {
        padding-right: 40px;
        color: #000000d9;
        font-size: 14px;
        display: block;
        line-height: 0px;
        width: 12px;
        height: 6px;
      }

      .time {
        display: flex;
        flex-direction: row;
        align-items: center;
        margin-left: 40px;
        font-size: 14px;

        span {
          margin-left: 8px;
          color: #00000073;
        }
      }

      .anony-box {
        font-size: 14px;
        line-height: 14px;
        color: #000000d9;
      }

      .ip-box {
        display: flex;
        flex-direction: row;
        align-items: center;
        line-height: 14px;
        font-size: 14px;
        margin-left: 2px;
      }

      .status {
        margin-left: 8px;
        padding: 5px 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 2px;
        font-size: 12px;
        line-height: 12px;
      }

      .status4 {
        display: flex;
        flex-direction: row;
        align-items: center;

        .icon-report {
          font-size: 16;
          display: block;
          line-height: 16px;
          color: #ff4d4fff;
        }

        span {
          color: #ff4d4fff;
          font-size: 14px;
          display: block;
          line-height: 14px;
          margin-left: 4px;
        }
      }

      .status1 {
        color: #d46b08;
        background: #fff7e6;
        border: 1px solid #ffd591;
      }

      .status2 {
        color: #389e0d;
        background: #f6ffed;
        border: 1px solid #b7eb8f;
      }

      .status3 {
        color: #cf1322;
        background: #fff1f0;
        border: 1px solid #ffa39e;
      }

      .check-single {
        line-height: 16px;
      }

      a {
        display: block;
        font-size: 14px;
        line-height: 14px;
      }

      .iconfont {
        display: block;
        height: 16px;
        line-height: 16px;
      }
    }

    .detail-header-count {
      justify-content: space-between;
    }

    .detail-content {
      display: flex;
      flex-direction: column;
      padding: 16px 16px 0;

      .commentator-comment1 {
        display: flex;
        flex-direction: row;
        justify-content: space-between;

        .commentator {
          display: flex;
          flex-direction: row;
          margin-right: 24px;

          .comment-left {
            font-size: 14px;
            color: rgba(0, 0, 0, 0.45);
          }

          .comment {
            margin-left: 12px;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.85);
          }
        }

        .like-box {
          display: flex;
          flex-direction: row;
          align-items: center;
          height: 18px;
          line-height: 21px;

          .iconfont {
            display: block;
            line-height: 18px;
            font-style: 18px;
          }

          .icon-a-Firstpush {
            font-size: 20px;
            line-height: 20px;
          }

          .like-count {
            margin-left: 6px;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.85);
          }
        }
      }

      .comment-box {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        margin-bottom: 16px;

        .commentator-comment {
          margin-top: 12px;
          display: flex;
          flex-direction: row;
          width: 1078px;

          .commentator {
            font-size: 14px;
            color: rgba(0, 0, 0, 0.45);
          }

          .comment {
            margin-left: 12px;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.85);
          }
        }
      }

      .replied {
        display: flex;
        flex-direction: row;
        justify-content: space-between;

        .replied-content-box {
          margin-right: 24px;
          display: flex;
          flex-direction: row;
          width: 100%;

          .replied-text {
            min-width: 36px;
            font-size: 14px;
            height: 22px;
            color: rgba(0, 0, 0, 0.45);
          }

          .replied-content-change {
            margin-left: 12px;
            display: flex;
            flex-direction: column;
            align-items: end;
            width: calc(100% - 44px);

            .text-input {
              width: 100%;
              position: relative;
            }

            .buttons {
              margin: 8px 0;
              display: flex;
              flex-direction: row;
              align-items: center;

              .button {
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
                width: 44px;
                height: 24px;
              }
            }
          }

          .replied-content {
            margin-bottom: 16px;
            margin-left: 12px;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.85);
          }
        }

        .like-box {
          display: flex;
          flex-direction: row;
          height: 22px;

          .iconfont {
            display: block;
            line-height: 18px;
            font-style: 18px;
          }

          .like-count {
            margin-left: 6px;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.85);
          }
        }
      }
    }

    .detail-footer {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      padding: 5px 16px;
      border-top: 1px solid rgba(0, 0, 0, 0.06);

      .footer-left {
        display: flex;
        flex-direction: row;
        align-items: center;

        .channal-name {
          display: block;
          font-size: 14px;
          line-height: 14px;
          color: #00000073;
          max-width: 348px;
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;

          .title {
            color: #000000d9;
          }

          .title:hover {
            cursor: pointer;
            color: #1f6ded;
          }
        }

        a {
          font-size: 14px;
          margin-left: 24px;
        }

        .all-count {
          margin-left: 4px;
          color: #00000073;
          font-size: 14px;
        }
      }

      .footer-right {
        display: flex;
        flex-direction: row;

        a {
          font-size: 14px;
          margin-right: 16px;
        }

        a:last-child {
          margin-right: 0;
        }
      }
    }

    .detail-content-footer {
      justify-content: end;
    }
  }

  .data-item:first-child {
    margin-top: 0;
  }
}

::v-deep(.ant-input-textarea-show-count > .ant-input) {
  height: 32px;
}

::v-deep(.ant-table-tbody > tr > td:last-child) {
  text-align: right;
}

::v-deep(.ant-badge-count, .ant-badge-dot, .ant-badge
    .ant-scroll-number-custom-component) {
  top: -12px;
  right: -30px;
}

::v-deep(.ant-input-textarea-show-count::after) {
  bottom: 8px;
}
</style>
<style>
@media screen and (max-width: 1439px) and (min-width: 1280px) {
  .marginL40_16 {
    margin-left: 16px;
  }
}

@media screen and (min-width: 1440px) {
  .marginL40_16 {
    margin-left: 40px;
  }
}

#commentmanage-container .table-bottom_fiexd {
  padding: 0 36px;
}
</style>
